package com.wzapp.sitposture.authentication.service.impl;

import com.alibaba.fastjson.JSON;
import com.wzapp.sitposture.authentication.pojo.entity.MobileAndCode;
import com.wzapp.sitposture.authentication.repository.MobileAndCodeRepository;
import com.wzapp.sitposture.authentication.service.MobileAndCodeService;
import com.wzapp.sitposture.common.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MobileAndCodeServiceImpl implements MobileAndCodeService {

    @Autowired
    MobileAndCodeRepository mobileAndCodeRepository;

    @Override
    public Long getExpireTime(String mobile) {

        MobileAndCode mobileAndCode = mobileAndCodeRepository.findTop1ByMobileAndDeletedOrderByUpdateTimeDesc(mobile,false);

        if (ToolUtil.isEmpty(mobileAndCode)) {
            return null;
        }

        return mobileAndCode.getExpireTime();
    }

    @Override
    public Integer count(String mobile, Long startTime,Long endTime) {
        return mobileAndCodeRepository.countByMobileAndUpdateTimeBetween(mobile, startTime,endTime);
    }

    @Override
    public boolean exists(String key) {
        MobileAndCode mobileAndCode = mobileAndCodeRepository.findTop1ByMobileAndDeletedOrderByUpdateTimeDesc(key,false);

        return ToolUtil.isNotEmpty(mobileAndCode);
    }

    @Override
    @Transactional
    public boolean remove(String key) {
        //删除前天的记录，并标记当天的记录为待删除，留存判断一天只能发送多少条
        mobileAndCodeRepository.deleteAllByMobile(key);
        mobileAndCodeRepository.updateAllByMobile(key);
        return true;
    }

    @Override
    @Transactional
    public boolean storage(String key, Object value, Long expireTime) {
        String v = "";
        if (ToolUtil.isNotEmpty(value) ) {
            Object json = JSON.toJSON(value);
            v = json.toString();
        }

        MobileAndCode mobileAndCode = new MobileAndCode();
        mobileAndCode.setMobile(key);
        mobileAndCode.setCode(v);
        mobileAndCode.setExpireTime(expireTime);
        mobileAndCode.setDeleted(false);
        mobileAndCodeRepository.save(mobileAndCode);
        mobileAndCodeRepository.flush();

        return false;
    }

    @Override
    public String queryMobileCodeByKey(String key) {

        MobileAndCode code = mobileAndCodeRepository.findTop1ByMobileAndDeletedOrderByUpdateTimeDesc(key,false);

        if (ToolUtil.isNotEmpty(code) && code.getExpireTime() > System.currentTimeMillis()) {
            return code.getCode();
        }

        return null;
    }

    public static void main(String[] args) {
        MobileAndCode mobileAndCode = new MobileAndCode();
        mobileAndCode.setMobile("1234213512");
        mobileAndCode.setCode("aaaa");
        mobileAndCode.setExpireTime(10002930921L);

        Object json = JSON.toJSON("abcd");
        System.out.println("-----------wzj-test-----------值=" + json.toString() +"," + "当前类=MobileAndCodeServiceImpl.main()");
    }
}
